<%@ page import="com.zjs.pojo.BcSubarea" %>
<%@ page import="com.zjs.vo.BcSubareaVo" %>
<%@ page import="java.util.Objects" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>管理分区</title>
<!-- 导入jquery核心类库 -->
<script type="text/javascript"
	src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
<!-- 导入easyui类库 -->
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath }/js/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath }/js/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath }/js/easyui/ext/portal.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath }/css/default.css">	
<script type="text/javascript"
	src="${pageContext.request.contextPath }/js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath }/js/easyui/ext/jquery.portal.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath }/js/easyui/ext/jquery.cookie.js"></script>
<script
	src="${pageContext.request.contextPath }/js/easyui/locale/easyui-lang-zh_CN.js"
	type="text/javascript"></script>

	<!--一键上传主件-->
	<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.ocupload-1.1.2.js"></script>
</head>
<body class="easyui-layout" style="visibility:hidden;">
	<div region="center" border="false">
    	<table id="grid"></table>
	</div>


	<!-- 添加 修改分区 -->
	<div class="easyui-window" title="分区添加修改" id="addSubareaWindow" collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px">
		<div style="height:31px;overflow:hidden;" split="false" border="false" >
			<div class="datagrid-toolbar">
				<a id="save" icon="icon-save" href="#" class="easyui-linkbutton" plain="true" >保存</a>
			</div>
		</div>
		
		<div style="overflow:auto;padding:5px;" border="false">
			<form id="subareaForm">
				<table class="table-edit" width="80%" align="center">
					<tr class="title">
						<td colspan="2">分区信息</td>
					</tr>
					<tr style="display:none;">
						<td>分拣编码</td>
						<!--,validType:remote['http://www.baudi.com','id'] class="easyui-validatebox"  data-options="required:true " validType="remote['${pageContext.request.contextPath}/bcSubarea/checkId.action','id']"-->
						<td><input type="text" name="id" /></td>
					</tr>
					<tr>
						<td>选择区域</td>
						<td>
							<input class="easyui-combobox" name="regionId"
    							data-options="valueField:'id',textField:'name',url:'${pageContext.request.contextPath}/bcRegion/findAll.action'" />
						</td>
					</tr>
					<tr>
						<td>关键字</td>
						<td><input type="text" name="addresskey" class="easyui-validatebox" required="true"/></td>
					</tr>
					<tr>
						<td>起始号</td>
						<td><input type="text" name="startnum" class="easyui-validatebox" required="true"/></td>
					</tr>
					<tr>
						<td>终止号</td>
						<td><input type="text" name="endnum" class="easyui-validatebox" required="true"/></td>
					</tr>
					<tr>
						<td>单双号</td>
						<td>
							<select class="easyui-combobox" name="single" style="width:150px;">  
							    <option value="0">单双号</option>  
							    <option value="1">单号</option>  
							    <option value="2">双号</option>  
							</select> 
						</td>
					</tr>
					<tr>
						<td>位置信息</td>
						<td><input type="text" name="position" class="easyui-validatebox" required="true" style="width:250px;"/></td>
					</tr>
				</table>
			</form>
		</div>
	</div>


	<!-- 查询分区 -->
	<div class="easyui-window" title="查询分区窗口" id="searchWindow" collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px">
		<div style="overflow:auto;padding:5px;" border="false">
			<form id="searchForm">
				<table class="table-edit" width="80%" align="center">
					<tr class="title">
						<td colspan="2">查询条件</td>
					</tr>
					<tr>
						<td>省</td>
						<td><input  class="easyui-combobox" name="province"
							data-options="valueField:'id',textField:'name',url:'${pageContext.request.contextPath}/bcSubarea/findProvinces.action'" />
                           </td>
					</tr>
					<tr>
						<td>市</td>
						<td><input class="easyui-combobox" name="city" data-options="valueField:'id',textField:'name',url:'${pageContext.request.contextPath}/bcSubarea/findCitys.action'"/></td>
					</tr>
					<tr>
						<td>区（县）</td>
						<td><input type="text" name="district"/></td>
					</tr>
					<tr>
						<td>关键字</td>
						<td><input type="text" name="addresskey"/></td>
					</tr>
					<tr>
						<td colspan="2"><a id="btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a> </td>
					</tr>
				</table>
			</form>
		</div>
	</div>


	<!--导出-->

	<div id="ExPortwin" class="easyui-window" title="导出文件位置" style="width:200px;height:100px"
		 data-options="iconCls:'icon-save',modal:true,closed: true, maximizable:false, minimizable:false">

		<form id="eximportFrom" style="position: relative; margin-left: 40px;">


			<input class="easyui-validatebox" data-options="required:true,missingMessage:'请输入保存文件名'"  name="saveFile"  style="width:100px; text-align: center;">


		</form>

		<a id="importBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-undo'" style="margin-left: 50px; margin-top: 10px">导出</a>


	</div>


	<script type="text/javascript">





		function doAdd(){
			//清除之前的数据
			$("#subareaForm").form('clear');
			$('#addSubareaWindow').window("open");
			// //开启验证信息
			// $("#subareaForm").form('enableValidation');
		}

		function doEdit(){

			var datagrid = $("#grid").datagrid('getSelections');


			if (datagrid.length<1){
				$.messager.alert("提示","你没有选中任何复选框!","error")

			} else if (datagrid.length==1) {


				var id=new Array();

				for (var i=0;i<datagrid.length;i++){
					id.push(datagrid[i].id)
				}

				var sid= id.join(",");


				$.ajax({
					url:'${pageContext.request.contextPath}/bcSubarea/upSubAreaById.action',
					type:'post',
					data:{id:sid},
					dataType:'json',
					success:function (obj) {


						$('#addSubareaWindow').window("open");

						$("#subareaForm").form('load',obj);



					}
				})


			}else  if (datagrid.length>1){

				$.messager.alert("提示","只能对一行进行修改","error");

				$("#grid").datagrid('uncheckAll')

			}

		}

		function doDelete(){
			var datagrid =$("#grid").datagrid('getSelections');

			//判断长度

			if (datagrid.length<1){

				$.messager.alert("提示","你没有选中复选框","error");

			} else{

				var ids=new Array();
				for (var i=0;i<datagrid.length;i++){
					ids.push(datagrid[i].id)
				}

				var strIds = ids.join(",");
				//ajax

				$.ajax({
					url:'${pageContext.request.contextPath}/bcSubarea/delSubAreaById.action',
					type:'post',
					data:{ids:strIds},
					dataType:'json',
					success:function (obj) {

						if (obj.success){

							//刷新table页面

							$("#grid").datagrid('reload');
							//清除选中的复选框
							$("#grid").datagrid("unselectAll");

							//提示
							$.messager.show({
								title:'消息提示',
								msg:'成功',
								timeout:1000,
								showType:'slide'
							})
						} else{

                           //刷新table页面

							$("#grid").datagrid('reload');

							//提示
							$.messager.show({
								title:'消息提示',
								msg:'失败',
								timeout:1000,
								showType:'slide'
							})
						}

					}
				})


			}

		}

		function doSearch(){
			$('#searchWindow').window("open");
			//清空查询的form表单
			$("#searchForm").form('clear');
		}

		//导出
		function doExport(){

		    window.location.href="${pageContext.request.contextPath}/bcSubarea/exPort.action";


		}







		//工具栏
		var toolbar = [ {
			id : 'button-search',
			text : '查询',
			iconCls : 'icon-search',
			handler : doSearch
		}, {
			id : 'button-add',
			text : '增加',
			iconCls : 'icon-add',
			handler : doAdd
		}, {
			id : 'button-edit',
			text : '修改',
			iconCls : 'icon-edit',
			handler : doEdit
		},{
			id : 'button-delete',
			text : '删除',
			iconCls : 'icon-cancel',
			handler : doDelete
		},{
			id : 'button-import',
			text : '导入',
			iconCls : 'icon-redo'
		},{
			id : 'button-export',
			text : '导出',
			iconCls : 'icon-undo',
			handler : doExport
		}];
		// 定义列
		var columns = [ [{
			field : 'id',
			checkbox : true,
		}, {
			field : 'decidedzoneId',
			title : '分拣编号',
			align : 'center'
		},{
			field : 'province',
			title : '省',
			width : 120,
			align : 'center',
			formatter : function(data,row ,index){

				return null==row.bcRegion?"":row.bcRegion.province;
			}
		}, {
			field : 'bcRegion.city',
			title : '市',
			width : 120,
			align : 'center',
			formatter : function(data,row ,index){
				return   null==row.bcRegion?"":row.bcRegion.city;
			}
		}, {
			field : 'district',
			title : '区',
			width : 120,
			align : 'center',
			formatter : function(data,row ,index){
				return null==row.bcRegion?"":row.bcRegion.district;
			}
		}, {
			field : 'addresskey',
			title : '关键字',
			width : 120,
			align : 'center'
		}, {
			field : 'startnum',
			title : '起始号',
			width : 100,
			align : 'center'
		}, {
			field : 'endnum',
			title : '终止号',
			width : 100,
			align : 'center'
		} , {
			field : 'single',
			title : '单双号',
			width : 100,
			align : 'center'
		} , {
			field : 'position',
			title : '位置',
			width : 200,
			align : 'center'
		} ] ];

		$(function(){






            //添加或者保存
			$("#save").click(function(){

				if($("#subareaForm").form("validate")){
					//表单提交
					$.ajax({
						url:"${pageContext.request.contextPath }/bcSubarea/save.action",
						type:"post",
						data:$("#subareaForm").serialize(),
						dataType:"json",
						success:function(obj){

							if (obj.success){

								$('#addSubareaWindow').window('close');


								//重新加载表格
								$("#grid").datagrid("reload");
								//提示成功
								$.messager.show({
									title:'消息提示',
									msg:'成功',
									timeout:1000,
									showType:'slide'
								});
								$("#grid").datagrid('uncheckAll')
							} else{

								 //提示出错
								$.messager.show({
									title:'消息提示',
									msg:'失败',
									timeout:1000,
									showType:'slide'
								});
								//关闭窗口
								$("#grid").datagrid('close')
							}
						}
					});


				}

			});


			$("#importBtn").bind('click',function () {

				if ($("#eximportFrom").form('validate')){

					$.ajax({
						url:'${pageContext.request.contextPath}/bcSubarea/exPort.action',
						type:'post',
						data:$("#eximportFrom").serialize(),
						dataType:'json',
						success:function (obj) {

							if (obj.success){

								//提示导出成功

								$.messager.show({
									title:'消息提示',
									msg:'导出成功,导出在了D盘下的student文件夹',
									timeout:1000,
									showType:'slide'
								});

								//关闭window提示框


								$("#ExPortwin").window('close');

								//清除input 里的文件名

								$("#eximportFrom").form('clear')



							} else{

								//提示导出失败  联系 管理员

								$.messager.show({
									title:'消息提示',
									msg:'导出失败,联系管理员',
									timeout:1000,
									showType:'slide'
								});

								//关闭window提示框
								$("#ExPortwin").window('close');

							}

						}
					})
				}


			});



			// 先将body隐藏，再显示，不会出现页面刷新效果
			$("body").css({visibility:"visible"});

			// 收派标准数据表格
			$('#grid').datagrid( {
				iconCls : 'icon-forward',
				fit : true,
				border : true,
				rownumbers : true,
				striped : true,
				pageList: [2,5,10,30,50,100],
				pagination : true,
				toolbar : toolbar,
				url : "${pageContext.request.contextPath}/bcSubarea/findPage.action",
				idField : 'id',
				columns : columns,
				onDblClickRow : doDblClickRow
			});

			// 添加、修改分区
			$('#addSubareaWindow').window({
				title: '添加修改分区',
				width: 600,
				modal: true,
				shadow: true,
				closed: true,
				height: 400,
				resizable:false
			});

			// 查询分区
			$('#searchWindow').window({
				title: '查询分区',
				width: 400,
				modal: true,
				shadow: true,
				closed: true,
				height: 400,
				resizable:false
			});


			$("#btn").click(function(){


			   $("#grid").datagrid('load',getFormData("searchForm"));
				<%--$.ajax({--%>
				<%--	url:'${pageContext.request.contextPath}/bcSubarea/searchSubArea.action',--%>
				<%--	type:'post',--%>
				<%--	data:$("#searchForm").serialize(),--%>
				<%--	dataType:'json',--%>
				<%--	success:function (obj) {--%>
				<%--		//关闭当前窗口--%>
						$("#searchWindow").window('close');


				<%--		$("#grid").datagrid('reload');--%>

				<%--		//清空查询的form表单--%>
				<%--		$("#searchForm").form('clear');--%>
				<%--	}--%>
				<%--})--%>

			});

//封装表单数据
            function getFormData(formId) {
                var form = document.getElementById(formId);
                var data = {};
                var tagElements = form.getElementsByTagName('input');

                for (var j = 0; j < tagElements.length; j++){
                    var input = tagElements[j];
                    var n = input.name;
                    var v = input.value;
                    data[n] = v;
                }
                return data;
            }



			//绑定一键上传按钮
			$("#button-import").upload({
				action:"${pageContext.request.contextPath}/bcSubarea/import.action",
				name:"excelFile",
				onComplete: function(response) {// 请求完成时 调用函数


					//刷新Table表单

					$("#grid").datagrid('reload');

					//提示导入成功
					$.messager.show({
						title:'消息提示',
						msg:'导入成功',
						timeout:1000,
						showType:'slide'
					})

				}

			});

		});

		function doDblClickRow(rowIndex, rowData){

			$.ajax({
				url:'${pageContext.request.contextPath}/bcSubarea/upSubAreaById.action',
				type:'post',
				data:{id:rowData.id},
				dataType:'json',
				success:function (obj) {


					$('#addSubareaWindow').window("open");

					$("#subareaForm").form('load',obj);

				}
			})
		}
	</script>
</body>
</html>